﻿#include "CPdfRptTool.h"


CPdfRptTool::CPdfRptTool(QObject *parent)
	: QObject(parent)
{
	m_dCellMinWidth = 140.0;//表格最小宽度
	m_dCellMinHeight = 30.0;//表格最小高度

	//A4纸的标准尺寸大约是 595×842 点
	if (m_dCellMinWidth > 0 && m_dCellMinHeight > 0)
	{
		m_dPageMaxCellWidthCnt = trunc(595.0 / m_dCellMinWidth);//页面最大表格宽度个数
		m_dPageMaxCellHeightCnt = trunc(842.0 / m_dCellMinHeight) - 1;//页面最大表格高度个数 预留空闲1个
	}
	
}

CPdfRptTool::~CPdfRptTool()
{


}

//定义最小单元格
void CPdfRptTool::defineMinCell(double dWidthVal, double dHeightVal)
{
	m_dCellMinWidth = dWidthVal;
	m_dCellMinHeight = dHeightVal;

	//A4纸的标准尺寸大约是 595×842 点
	if (m_dCellMinWidth > 0 && m_dCellMinHeight > 0)
	{
		m_dPageMaxCellWidthCnt = trunc(595 / m_dCellMinWidth);//页面最大表格宽度个数
		m_dPageMaxCellHeightCnt = trunc(842 / m_dCellMinHeight) - 1;//页面最大表格高度个数  预留空闲1个
	}
	
}

//设置字体和画笔等级
void CPdfRptTool::setFontAndPenLevel(EPdfRptFontLevel eFontLevel, EPdfRptPenLevel ePenLevel)
{
	m_eFontLevel = eFontLevel;
	m_ePenLevel = ePenLevel;
}



//绘制表格附带文字
bool CPdfRptTool::drawFormAttachText(double &dStartRow, double &dStartCol, double dWidthsCnt, double dHeightsCnt, QString sText, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	QRect mergedCellRect(8 + int(dStartCol * m_dCellMinWidth), int(dStartRow * m_dCellMinHeight), int(m_dCellMinWidth * dWidthsCnt), int(m_dCellMinHeight * dHeightsCnt));//表格预留页边8的距离
	QPen penNew;
	penNew.setColor(Qt::black);
	penNew.setWidth(1);
	painter.setPen(penNew);
	painter.drawRect(mergedCellRect);//绘制单元格边框
	painter.setFont(getExpFont(m_eFontLevel));
	painter.setPen(getExpPen(m_ePenLevel));
	painter.drawText(mergedCellRect, Qt::TextWordWrap | Qt::AlignCenter, sText);  // 居中绘制单元格内容

	retNextRowAndCol(dStartRow, dStartCol, dWidthsCnt, dHeightsCnt);//返回下一个行列(绘制完返回下一个可用于绘制的行列号)
	return true;
}

//绘制表格附带图片
bool CPdfRptTool::drawFormAttachPic(double &dStartRow, double &dStartCol, double dWidthsCnt, double dHeightsCnt, QPixmap &pixmap, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{

	painter.setFont(getExpFont(m_eFontLevel));
	painter.setPen(getExpPen(m_ePenLevel));


	QRect mergedCellRect(8 + int(dStartCol * m_dCellMinWidth), int(dStartRow * m_dCellMinHeight), int(m_dCellMinWidth * dWidthsCnt), int(m_dCellMinHeight * dHeightsCnt));//表格预留页边8的距离
	painter.drawRect(mergedCellRect);//绘制单元格边框
	painter.drawPixmap(mergedCellRect, pixmap);

	retNextRowAndCol(dStartRow, dStartCol, dWidthsCnt, dHeightsCnt);//返回下一个行列(绘制完返回下一个可用于绘制的行列号)
	

	return true;
}

//判断执行自动换页
bool CPdfRptTool::judgeExecAutoPageChg(double &dStartRow, double &dStartCol, int nNeedRows, QPrinter &printer, QString &sErrMsg)
{
	int nOnePageBriefCnt = m_dPageMaxCellHeightCnt;//一页可显示的数量
	int nStartRow = (int)dStartRow;
	int nRestRowCnt = nOnePageBriefCnt - nStartRow;//剩余可用行
	if (nNeedRows > nRestRowCnt)
	{
		printer.newPage(); // 添加新的一页
		dStartRow = 0;
		dStartCol = 0;

		return true;
	}

	return false;
}

//绘制单文本(指定行列位置)
bool CPdfRptTool::drawSingleText(int nStartRow, int nStartCol, EPdfRptFontLevel eFontLevel, EPdfRptPenLevel ePenLevel, QString sText, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	//暂时没用到
	

	return true;
}

//绘制单文本(指定枚举的位置)
bool CPdfRptTool::drawSingleText(EPdfRptComPosition ePosition, QString sText, QPainter &painter, QPrinter &printer, QString &sErrMsg)
{
	painter.setFont(getExpFont(m_eFontLevel));
	painter.setPen(getExpPen(m_ePenLevel));
	double dStartRow = 0;//起始行
	double dStartCol = 0;//起始列
	getExpPosition(ePosition, dStartRow, dStartCol);
	
	painter.drawText(double(dStartCol * m_dCellMinWidth), double(dStartRow * m_dCellMinHeight), sText);

	return true;
}

//绘制表格线加粗
bool CPdfRptTool::drawFormLineBold(int nStartRow, int nStartCol, int nWidths, int nHeights, QString &sErrMsg)
{

	return true;
}

//获取页面最大表格高度个数
double CPdfRptTool::getPageMaxCellHeightCnt()
{
	return m_dPageMaxCellHeightCnt;
}

//返回下一个行列(绘制完返回下一个可用于绘制的行列号)
void CPdfRptTool::retNextRowAndCol(double &dStartRow, double &dStartCol, double dWidthsCnt, double dHeightsCnt)
{
	dStartCol += dWidthsCnt;
	if (dStartCol >= m_dPageMaxCellWidthCnt)//目前4格宽度最大了
	{
		dStartCol = 0;

		dStartRow += dHeightsCnt;

	}
}

//获取字体(根据字体等级)
QFont CPdfRptTool::getExpFont(EPdfRptFontLevel eFontLevel)
{
	QFont fontNew;
	fontNew.setFamily("Arial");

	switch (eFontLevel)
	{
	case enPdfRptFontLev13:
	{
		fontNew.setPointSize(13);//设置字体大小
		fontNew.setBold(false); // 是否设置为加粗
	}
		break;
	case enPdfRptFontLev13Bold:
	{
		fontNew.setPointSize(13);//设置字体大小
		fontNew.setBold(true); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev18:
	{
		fontNew.setPointSize(18);//设置字体大小
		fontNew.setBold(false); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev18Bold:
	{
		fontNew.setPointSize(18);//设置字体大小
		fontNew.setBold(true); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev24:
	{
		fontNew.setPointSize(24);//设置字体大小
		fontNew.setBold(false); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev24Bold:
	{
		fontNew.setPointSize(24);//设置字体大小
		fontNew.setBold(true); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev30:
	{
		fontNew.setPointSize(30);//设置字体大小
		fontNew.setBold(false); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev30Bold:
	{
		fontNew.setPointSize(30);//设置字体大小
		fontNew.setBold(true); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev40:
	{
		fontNew.setPointSize(40);//设置字体大小
		fontNew.setBold(false); // 是否设置为加粗
	}
	break;
	case enPdfRptFontLev40Bold:
	{
		fontNew.setPointSize(40);//设置字体大小
		fontNew.setBold(true); // 是否设置为加粗
	}
	break;
	default:
	{
		fontNew.setPointSize(12);//设置字体大小
		fontNew.setBold(false); // 是否设置为加粗
	}
		break;
	}
	


	return fontNew;
}

//获取画笔(根据画笔等级)
QPen CPdfRptTool::getExpPen(EPdfRptPenLevel ePenLevel)
{
	QPen penNew;
	penNew.setStyle(Qt::SolidLine);// 实线

	switch (ePenLevel)
	{
	case enPdfRptPenBlack:
	{
		penNew.setColor(Qt::black);
		penNew.setWidth(1);
	}
	break;
	case enPdfRptPenBlackBold:
	{
		penNew.setColor(Qt::black);
		penNew.setWidth(3);
	}
	break;
	case enPdfRptPenRed:
	{
		penNew.setColor(Qt::red);
		penNew.setWidth(1);
	}
	break;
	case enPdfRptPenRedBold:
	{
		penNew.setColor(Qt::red);
		penNew.setWidth(3);
	}
	break;
	default:
	{
		penNew.setColor(Qt::black);
		penNew.setWidth(1);
	}
	break;
	}



	return penNew;

}

void CPdfRptTool::getExpPosition(EPdfRptComPosition ePosition, double &dRow, double &dCol)
{
	switch (ePosition)
	{
	case enPdfRptPosCusCpy:
	{

	}
	break;
	case enRobPdfRptPosName://机器人报告名称位置
	{
		dRow = 4;
		dCol = 0.5;
	}
	break;
	case enPdfPosSinglePointName://单个巡检点名称位置
	{
		dRow = 0.5;
		dCol = 0.06;
	}
	break;
	case enSynPdfRptPosName://综合报告名称位置
	case enRecPdfRptPosName://纠偏报告名称位置
	case enTearPdfRptPosName://撕裂报告名称位置
	case enRestAiPdfRptPosName://其他AI检测
	{
		dRow = 4;
		dCol = 1;
	}
	break;
	default:
		break;
	}
}
